package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.LoginService;
import cn.gson.boot.model.service.MainPageService;
import cn.gson.boot.model.service.WenDaService;
import com.alibaba.druid.support.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/mainpage")
//@SessionAttributes({"user"})
public class MainPageController {

    @Autowired
    MainPageService mainPageService;

    @RequestMapping("/sevenday")
    @ResponseBody
    public Map<String,Object> sevenDay(HttpSession session){
        Map<String,Object> map = new HashMap<>();
        List<String> pastSevenDaysList = getDaysBetwwen(6);//获取最近七天的时间
        User user = (User) session.getAttribute("user");
        List<Integer> shujuList = new ArrayList<>();
        List<Integer> shujuList2 = new ArrayList<>();
        for(int i=0;i<pastSevenDaysList.size();i++){
            shujuList.add(mainPageService.selectEmpSevenDayKehu(pastSevenDaysList.get(i),user.getEmpId()));
            shujuList2.add(mainPageService.selectEmpSevenDayDingDan(pastSevenDaysList.get(i),user.getEmpName()));
        }
        map.put("time",pastSevenDaysList);
        map.put("shujuKh",shujuList);
        map.put("shujuDd",shujuList2);
        return map;
    }

    private Date getDateAdd(int days){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -days);
        return c.getTime();
    }
    private List<String> getDaysBetwwen(int days){
        List<String> dayss = new ArrayList<>();
        Calendar start = Calendar.getInstance();
        start.setTime(getDateAdd(days));
        Long startTIme = start.getTimeInMillis();
        Calendar end = Calendar.getInstance();
        end.setTime(new Date());
        Long endTime = end.getTimeInMillis();
        Long oneDay = 1000 * 60 * 60 * 24l;
        Long time = startTIme;
        while (time <= endTime) {
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            dayss.add(df.format(d));
            time += oneDay;
        }
        return dayss;
    }

    @RequestMapping("/sevenDayZhuxing")
    @ResponseBody
    public Map<String,Object> sevenDayZhuxing(){
        Map<String,Object> map = new HashMap<>();
        List<Integer> in = new ArrayList<>();
        List<String> str = new ArrayList<>();
        List<String> pastSevenDaysList = getDaysBetwwen(6);//获取最近七天的时间
        Integer ht = 0;//合同
        Integer dd = 0;//订单
        Integer hk = 0;//回款
        Integer th = 0;//退货
        Integer hh = 0;//换货
        for(int i=0;i<pastSevenDaysList.size();i++){
           /* System.err.println("ht:"+ht);
            System.err.println("dd:"+dd);
            System.err.println("hk:"+hk);
            System.err.println("th:"+th);
            System.err.println("hh:"+hh);*/
            ht += mainPageService.selectSevenDayHeTong(pastSevenDaysList.get(i));
            dd += mainPageService.selectSevenDayDingDan(pastSevenDaysList.get(i));
            hk += mainPageService.selectSevenDayHuiKuan(pastSevenDaysList.get(i));
            th += mainPageService.selectSevenDayTuiHuo(pastSevenDaysList.get(i));
            hh += mainPageService.selectSevenDayHuanHuo(pastSevenDaysList.get(i));
        }
        in.add(ht);
        str.add("合同");
        in.add(dd);
        str.add("订单");
        in.add(hk);
        str.add("回款");
        in.add(th);
        str.add("退货");
        in.add(hh);
        str.add("换货");
        map.put("inn",in);
        map.put("str",str);
        return map;
    }

    @RequestMapping("/oneOne")
    @ResponseBody
    public List<Integer> oneOne(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Integer> list = new ArrayList<>();
        list.add(mainPageService.selectEmpKehu(user.getEmpName()));//查询员工负责的首所有客户
        list.add(mainPageService.selectEmpHetong(user.getEmpName()));//查询员工负责的所有合同
        list.add(mainPageService.selectEmpDingDan(user.getEmpName()));//查询员工负责的所有订单
        return list;
    }


//====================================================================================

    //排行榜
    @RequestMapping("/selectPaiHang")
    @ResponseBody
    public List<PaiHangBang> paiHangBang(String tj){

        List<String> list = new ArrayList<>();
        List<String> dataList = new ArrayList<>();
        if(tj.equals("本周")){
            dataList = getTimeInterval();//本周的日期
        }else if(tj.equals("本月")){
            dataList = getMonthFullDay();
        }

        List<String> empNameList = mainPageService.selectAllEmpNameQvChong();//所有员工
        List<Integer> numList = new ArrayList<>();// 数量集合
        int nn = 0;
        for(int e=0;e<empNameList.size();e++){
            for(int w=0;w<dataList.size();w++){
                nn += mainPageService.selectPaiHang(dataList.get(w),empNameList.get(e));
            }
            list.add(empNameList.get(e));
            numList.add(nn);
            nn = 0;
        }

        for (int i = 0; i < numList.size() - 1; i++) {
            for (int j = 1; j < numList.size() - i; j++) {
                Integer a;
                String b;
                if ((numList.get(j)).compareTo(numList.get(j-1)) > 0) { // 比较两个整数的大小
                    a = numList.get(j);
                    numList.set((j), numList.get(j-1));
                    numList.set(j-1, a);

                    b = list.get(j);
                    list.set((j), list.get(j-1));
                    list.set(j-1, b);
                }
            }
        }
        List<PaiHangBang> phList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            phList.add(new PaiHangBang(list.get(i),numList.get(i)));
        }

       /* map.put("name",list);
        map.put("num",numList);*/

        return phList;
    }


    //获取本周的所有日期
    public List<String> getTimeInterval() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
        }
        List<String> dates = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            dates.add(dateFormat.format(calendar.getTime()));
            calendar.add(Calendar.DATE, 1);
        }
        return dates;
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    //获取本月的所有日期
    public List<String> getMonthFullDay() {
        Date date1 = new Date();
        String date = sdf.format(date1);
        List<String> fullDayList = new ArrayList<String>();
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        int day = 1;// 所有月份从1号开始
        Calendar cal = Calendar.getInstance();// 获得当前日期对象
        cal.clear();// 清除信息
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);// 1月从0开始
        cal.set(Calendar.DAY_OF_MONTH, day);
        int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int j = 0; j <= (count - 1); ) {
            if (sdf.format(cal.getTime()).equals(getLastDay(year, month)))
                break;
            cal.add(Calendar.DAY_OF_MONTH, j == 0 ? +0 : +1);
            j++;
            fullDayList.add(sdf.format(cal.getTime()));
        }
        return fullDayList;
    }

    public String getLastDay(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        return sdf.format(cal.getTime());
    }


    @RequestMapping("/yunYing")
    @ResponseBody
    public List<Integer> yunYing(String yy){
        List<Integer> intt = new ArrayList<>();
        List<String> dataList = new ArrayList<>();
        if(yy.equals("本周")){
            dataList = getTimeInterval();
        }else if(yy.equals("本月")){
            dataList = getMonthFullDay();
        }else if(yy.equals("本年")){
            String format = sdf.format(new Date());
            String[] split = format.split("-");
            dataList.add(split[0]);
        }
        Integer ht = 0;
        Integer dd = 0;
        Integer kh = 0;
        for(int i=0;i<dataList.size();i++){
            ht += mainPageService.selectHeTongByTime(dataList.get(i));
            dd += mainPageService.selectDingDanByTime(dataList.get(i));
            kh += mainPageService.selectKeHuByTime(dataList.get(i));
        }
        intt.add(ht);
        intt.add(dd);
        intt.add(kh);
        return intt;
    }


    @Autowired
    WenDaService wenDaService;

    @RequestMapping("/wendaa")
    @ResponseBody
    public List<Wenda> wendaa(String qs){
        return wenDaService.selecAllWenDa(qs);
    }

}
